import { createRouter, createWebHistory } from 'vue-router'
import store from '@/store'
import Login from '../views/Login.vue'
import DashboardLayout from '../views/DashboardLayout.vue'
import DashboardHome from '../views/DashboardHome.vue'
import ProcessList from '../views/ProcessList.vue'
import ProcessEditor from '../views/ProcessEditor.vue'
import Settings from '../views/Settings.vue'
import BrowserAgent from '../components/automation/BrowserAgent.vue'
import WingknifeProcess from '../views/WingknifeProcess.vue'
import WingknifeExecutions from '../views/WingknifeExecutions.vue'
import DataCleanTasks from '../views/DataCleanTasks.vue'
import ResultPreview from '../views/ResultPreview.vue'
import BrandManagement from '../views/config/BrandManagement.vue'
import BrandExtractorPage from '../views/BrandExtractorPage.vue'
import BrandExtractorTool from '../views/BrandExtractorTool.vue'

// 配置管理相关组件
import ProductNameManagement from '../views/config/ProductNameManagement.vue'
import UnitManagement from '../views/config/UnitManagement.vue'
import UnitConversionManagement from '../views/config/UnitConversionManagement.vue'
import MarketingWordManagement from '../views/config/MarketingWordManagement.vue'
import SystemConfigManagement from '../views/config/SystemConfigManagement.vue'

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { 
      requiresAuth: false,
      title: '登录 - 基于ai的电商数据采集分析系统'
    }
  },
  {
    path: '/',
    component: DashboardLayout,
    meta: { requiresAuth: true },
    children: [
      {
        path: '',
        name: 'DashboardHome',
        component: DashboardHome,
        meta: { 
          requiresAuth: true,
          title: '首页 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'process',
        name: 'ProcessList',
        component: ProcessList,
        meta: { 
          requiresAuth: true,
          title: '流程管理 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'process/editor/:id',
        name: 'ProcessEditor',
        component: ProcessEditor,
        props: true,
        meta: { 
          requiresAuth: true,
          title: '流程编辑 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'process/editor',
        name: 'NewProcess',
        component: ProcessEditor,
        meta: { 
          requiresAuth: true,
          title: '新建流程 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'automation',
        name: 'BrowserAgent',
        component: BrowserAgent,
        meta: { 
          requiresAuth: true,
          title: '浏览器自动化 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'settings',
        name: 'Settings',
        component: Settings,
        meta: { 
          requiresAuth: true,
          title: '系统设置 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'wingknife',
        name: 'WingknifeProcess',
        component: WingknifeProcess,
        meta: { 
          requiresAuth: true,
          title: '影刀流程管理 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'wingknife/executions',
        name: 'WingknifeExecutions',
        component: WingknifeExecutions,
        meta: { 
          requiresAuth: true,
          title: '流程运行管理 - 基于ai的电商数据采集分析系统'
        }
      },
      // 兼容旧路由
      {
        path: 'brands',
        redirect: '/config/brands',
        meta: { requiresAuth: true }
      },
      
      {
        path: 'sku',
        component: () => import('../views/sku/SkuLayout.vue'),
        meta: { requiresAuth: true, title: 'SKU管理' },
        redirect: '/sku/list',
        children: [
          {
            path: 'list',
            name: 'SKUList',
            component: () => import('@/views/sku/SkuList.vue'),
            meta: { title: 'SKU列表 - SKU管理' }
          }
        ]
      },
      // 配置管理相关路由
      {
        path: 'config',
        name: 'ConfigManagement',
        component: () => import('../views/config/ConfigLayout.vue'),
        meta: { 
          requiresAuth: true,
          title: '配置管理 - 基于ai的电商数据采集分析系统'
        },
        children: [
          {
            path: 'brands',
            name: 'BrandManagement',
            component: BrandManagement,
            meta: { 
              requiresAuth: true,
              title: '品牌管理 - 基于ai的电商数据采集分析系统'
            }
          },
          {
            path: 'product-names',
            name: 'ProductNameManagement',
            component: ProductNameManagement,
            meta: { 
              requiresAuth: true,
              title: '名称管理 - 基于ai的电商数据采集分析系统'
            }
          },
          {
            path: 'units',
            name: 'UnitManagement',
            component: UnitManagement,
            meta: { 
              requiresAuth: true,
              title: '单位管理 - 基于ai的电商数据采集分析系统'
            }
          },
          {
            path: 'unit-conversions',
            name: 'UnitConversionManagement',
            component: UnitConversionManagement,
            meta: { 
              requiresAuth: true,
              title: '单位换算管理 - 基于ai的电商数据采集分析系统'
            }
          },
          {
            path: 'marketing-words',
            name: 'MarketingWordManagement',
            component: MarketingWordManagement,
            meta: { 
              requiresAuth: true,
              title: '营销词管理 - 基于ai的电商数据采集分析系统'
            }
          },
          {
            path: 'system',
            name: 'SystemConfigManagement',
            component: SystemConfigManagement,
            meta: { 
              requiresAuth: true,
              title: '系统配置 - 基于ai的电商数据采集分析系统'
            }
          }
        ]
      },
      {
        path: 'data-clean',
        name: 'DataCleanTasks',
        component: DataCleanTasks,
        meta: { 
          requiresAuth: true,
          title: '数据清洗 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'data-clean-result-preview/:id',
        name: 'ResultPreview',
        component: ResultPreview,
        props: true,
        meta: { 
          requiresAuth: true,
          title: '清洗结果预览 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'brand-extractor',
        name: 'BrandExtractor',
        component: BrandExtractorPage,
        meta: { 
          requiresAuth: true,
          title: '品牌提取工具 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'brand-extractor-tool',
        name: 'BrandExtractorTool',
        component: BrandExtractorTool,
        meta: { 
          requiresAuth: true,
          title: '品牌提取工具 - 基于ai的电商数据采集分析系统'
        }
      },
      {
        path: 'item',
        name: 'ItemManagement',
        component: () => import('@/views/item/ItemList'),
        meta: { 
          requiresAuth: true,
          title: '商品管理 - 基于ai的电商数据采集分析系统',
          icon: 'el-icon-goods'
        }
      },
      {
        path: 'shop',
        name: 'ShopManagement',
        component: () => import('@/views/shop/ShopList'),
        meta: { 
          requiresAuth: true,
          title: '店铺管理 - 基于ai的电商数据采集分析系统',
          icon: 'el-icon-shop'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 导航守卫
router.beforeEach((to, from, next) => {
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  } else {
    document.title = '基于ai的电商数据采集分析系统'
  }

  const isAuthenticated = store.getters.isAuthenticated
  
  // 如果路由需要认证
  if (to.matched.some(record => record.meta.requiresAuth)) {
    // 如果未登录，重定向到登录页
    if (!isAuthenticated) {
      next({
        path: '/login',
        query: { redirect: to.fullPath }
      })
    } else {
      next()
    }
  } else {
    // 如果已登录且访问登录页，重定向到首页
    if (to.path === '/login' && isAuthenticated) {
      next('/')
    } else {
      next()
    }
  }
})

export default router 